package com.service.single.example.file;

import cn.hutool.poi.word.Word07Writer;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
public class DocxHandle {


    /**
     * 生成docx文件
     */
    public void exportDocx(HttpServletResponse response) {
        String title = "文件标题";
        Word07Writer writer = new Word07Writer();
        writer.addText(ParagraphAlignment.CENTER, new Font("宋体", Font.PLAIN, 16), title);
        String content = "文件内容";
        writer.addText(new Font("宋体", Font.PLAIN, 16), content);
        String fileName = "文件名称" + ".docx";
        try {
            response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
            ServletOutputStream os = response.getOutputStream();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/msword");
            writer.flush(os);
            os.close();
        } catch (IOException e) {
            log.error(e.toString());
        }
    }


    /**
     * 按段落 读取 docx
     */
    @SneakyThrows
    private void readDocx(MultipartFile file) {
        InputStream inputStream = file.getInputStream();
        XWPFDocument document = new XWPFDocument(inputStream);
        // 读取段落
        List<XWPFParagraph> paragraphs = document.getParagraphs();
        for (XWPFParagraph paragraph : paragraphs) {
            String text = paragraph.getParagraphText();
            System.out.println(text);
        }
    }

    /**
     * 按段落 读取 doc
     */
    @SneakyThrows
    private void readDoc(MultipartFile file) {
        InputStream inputStream = file.getInputStream();
        HWPFDocument document = new HWPFDocument(inputStream);
        Range range = document.getRange();
        for (int i = 0; i < range.numParagraphs(); i++) {
            Paragraph paragraph = range.getParagraph(i);
            String text = paragraph.text();
            System.out.println(text);
        }
    }

    /**
     * 读取 pdf 文件内容
     */
    private String readPDF(MultipartFile file) {
        StringBuilder content = new StringBuilder();
        try {
            InputStream inputStream = file.getInputStream();
            RandomAccessBuffer buffer = new RandomAccessBuffer(inputStream);
            PDFParser pdfParser = new PDFParser(buffer);
            pdfParser.parse();
            PDDocument document = pdfParser.getPDDocument();
            // 获取页码
            int pages = document.getNumberOfPages();
            PDFTextStripper stripper = new PDFTextStripper();
            // 设置按顺序输出
            stripper.setSortByPosition(true);
            stripper.setStartPage(1);
            stripper.setEndPage(pages);
            content.append(stripper.getText(document));
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();

    }


}
